package com.cyou.qb.front.jmx;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cyou.qb.constants.Constants;

public class FrontConstants implements FrontConstantsMBean{
	private static Log log =LogFactory.getLog(FrontConstants.class);
	//是否需要高频访问控制
	public static boolean FREQUENCY_INTERCEPTOR=false;
	
	//是高频访问控制时间片 单位秒
	public static int FREQUENCY_TIME_SEGMENT_SECOND=10;
	
	//是高频访问控制时间片内的最大次数
	public static int FREQUENCY_TIMES=100;
	
	public static String RES_URL_PREFIX;
	
	public static String POST_URL_PREFIX;
	
	public static String AJAX_URL_PREFIX;
	

	public static Properties pt = null;

    static {
        pt = new Properties();
        InputStream ist = null;
        try {
            ist = Constants.class.getClassLoader().getResourceAsStream("system.properties");
            pt.load(ist);
        	FREQUENCY_INTERCEPTOR = Boolean.parseBoolean(pt.getProperty("FREQUENCY_INTERCEPTOR"));
        	FREQUENCY_TIME_SEGMENT_SECOND =Integer.parseInt(pt.getProperty("FREQUENCY_TIME_SEGMENT_SECOND"));
        	FREQUENCY_TIMES =Integer.parseInt(pt.getProperty("FREQUENCY_TIMES"));
        	
        	RES_URL_PREFIX = pt.getProperty("RES_URL_PREFIX");
        	POST_URL_PREFIX = pt.getProperty("POST_URL_PREFIX");
        	AJAX_URL_PREFIX = pt.getProperty("AJAX_URL_PREFIX");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("读取dynamic-constants.properties 出错",e);
        } finally {
            try {
            	if(null != ist) {
            		ist.close();
            	}
            } catch (Exception e) {
            }
        }

    }

    private void updateProperties() {
        FileOutputStream fost = null;
        try {
            fost = new FileOutputStream(new File(Constants.class.getClassLoader().getResource("system.properties").getPath()));
            pt.store(fost, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fost.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
	
	@Override
	public boolean getFrequencyInterceptor() {
		return FrontConstants.FREQUENCY_INTERCEPTOR;
	}

	@Override
	public void setFrequencyInterceptor(boolean frequencyInterceptor) {
		FREQUENCY_INTERCEPTOR=frequencyInterceptor;
		pt.put("FREQUENCY_INTERCEPTOR", frequencyInterceptor+"");
		updateProperties();
	}

	@Override
	public int getFrequencyTimeSegmentSecond() {
		return FREQUENCY_TIME_SEGMENT_SECOND;
	}

	@Override
	public void setFrequencyTimeSegmentSecond(int frequencyTimeSegmentSecond) {
		FREQUENCY_TIME_SEGMENT_SECOND=frequencyTimeSegmentSecond;
		pt.put("FREQUENCY_TIME_SEGMENT_SECOND", frequencyTimeSegmentSecond+"");
		updateProperties();
	}

	@Override
	public int getFrequencyTimes() {
		return FREQUENCY_TIMES;
	}

	@Override
	public void setFrequencyTimes(int frequencyTimes) {
		FREQUENCY_TIMES=frequencyTimes;
		pt.put("FREQUENCY_TIMES", frequencyTimes+"");
		updateProperties();
	}
	
}
